package com.sxhalo.fsms.controller.api;

import java.sql.Timestamp;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sxhalo.fsms.controller.BaseController;
import com.sxhalo.fsms.domain.FactPatrolJobItem;
import com.sxhalo.fsms.log.SysControllerLog;
import com.sxhalo.fsms.service.IFactPatrolJobItemService;
import com.sxhalo.fsms.util.ApiResult;

@Controller
@RequestMapping("/api")
public class FactPatrolJobItemController extends BaseController {

	@Autowired
	private IFactPatrolJobItemService itemService;
	
	@ResponseBody
	@RequestMapping(value = "/patroljob/item", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	public ApiResult findPatrol(Integer id, Integer current, Integer pageSize, HttpServletRequest req) {
		String where = "delete_flag=0";
		String order = "create_time desc";
		if (id != null) {
			where += " and job_id=" + id;
		}
		return findList(current, pageSize, where, order, itemService);
	}
	
	@ResponseBody
	@SysControllerLog(operationName = "新增巡检任务项目", modelName = "巡检任务项目")
	@RequestMapping(value = "/patroljob/item", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ApiResult savePatrol(@RequestBody FactPatrolJobItem record, HttpServletRequest req) {
		Timestamp nowtime = new Timestamp(System.currentTimeMillis());
		Integer maxId = itemService.getMaxId();
		record.setId(maxId + 1);
		record.setIndex(maxId + 1);
		record.setCreateTime(nowtime);
		record.setUpdateTime(nowtime);
		record.setDeleteFlag(0);
		itemService.insertSelective(record);
		return ApiResult.retOK(req);
	}
	
	@ResponseBody
	@SysControllerLog(operationName = "编辑巡检任务项目", modelName = "巡检任务项目")
	@RequestMapping(value = "/patroljob/item", method = RequestMethod.PUT, produces="application/json;charset=UTF-8")
	public ApiResult updatePatrol(@RequestBody  FactPatrolJobItem record, HttpServletRequest req) {
		Timestamp nowtime = new Timestamp(System.currentTimeMillis());
		record.setUpdateTime(nowtime);
		itemService.updateByPrimaryKeySelective(record);
		return ApiResult.retOK(req);
	}
	
	@ResponseBody
	@SysControllerLog(operationName = "删除工厂巡检任务", modelName = "工厂巡检任务")
	@RequestMapping(value = "/patroljob/item", method = RequestMethod.DELETE, produces = "application/json;charset=UTF-8")
	public ApiResult delPatrol(Integer id, HttpServletRequest req) {
		Timestamp nowtime = new Timestamp(System.currentTimeMillis());
		FactPatrolJobItem record = new FactPatrolJobItem();
		record.setId(id);
		record.setDeleteFlag(1);
		record.setUpdateTime(nowtime);
		itemService.updateByPrimaryKeySelective(record);
		return ApiResult.retOK(req);
	}
}
